package tournamentmanager;

import java.io.*;

public class Util {

    private Util() {
    }

    public static Object readFromFile(String fileName) throws IOException, ClassNotFoundException {
        FileInputStream in = null;
        ObjectInputStream s = null;
        try {
            in = new FileInputStream(fileName);
            s = new ObjectInputStream(in);
            return s.readObject();
        }
        finally {
            if (in != null) {
                try {
                    in.close();
                }
                catch (IOException e) {
                    //do nothing
                }
            }
            if (s != null) {
                try {
                    s.close();
                }
                catch (IOException e) {
                    //do nothing
                }
            }
        }
    }

    public static void saveToFile(Object object, String fileName) throws IOException {
        FileOutputStream out = null;
        ObjectOutputStream s = null;
        try {
            out = new FileOutputStream(fileName);
            s = new ObjectOutputStream(out);
            s.writeObject(object);
            s.flush();
        }
        finally {
            if (out != null) {
                try {
                    out.close();
                }
                catch (IOException e) {
                    //do nothing
                }
            }
            if (s != null) {
                try {
                    s.close();
                }
                catch (IOException e) {
                    //do nothing
                }
            }
        }
    }

}
